<template>
  <van-button
   v-if="isFollowed"
   round
   :loading="isFollowLoading"
   size="small"
   @click="handleFollow"
 >已关注</van-button>
 <van-button
   v-else
   color="#3296fa"
   round
   :loading="isFollowLoading"
   size="small"
   icon="plus"
   @click="handleFollow"
 >关注</van-button>
</template>

<script>
import { addFollow, deleteFollow } from '@/api/user'
export default {
  name: 'FollowUser',
  data () {
    return {
      isFollowLoading: false
    }
  },
  props: {
    isFollowed: {
      type: Boolean,
      required: true
    },
    userId: {
      type: [Number, String, Object],
      required: true
    }
  },
  methods: {
    async handleFollow () {
      try {
        if (this.isFollowed) { // 已关注，取消关注
          this.isFollowLoading = true
          await deleteFollow(this.userId)
        } else { // 未关注，关注
          this.isFollowLoading = true
          await addFollow(this.userId)
        }
      } catch (err) {
        this.$toast('操作失败，请稍后再试')
      }
      this.isFollowLoading = false
      this.$emit('changeFollow')
    }
  }
}
</script>

<style>

</style>
